package app

import app.Main.{isValidFileName, parentDir}
import command.CommandExec
import implicits.Implicits._
import org.apache.commons.io.FileUtils
import org.joda.time.Years
import tool.Pojo.CommandData
import tool.Tool
import utils.Utils

import java.io.File
import scala.collection.immutable.SeqMap
import scala.collection.parallel.CollectionConverters._

/**
 * Created by yz on 29/10/2021
 */
object Main {

  val parentDir = new File("E:\\crc_database\\test")
  val dirMap = SeqMap(
    //    "Germline_mutation" -> "携带胚系突变",
    //    "Germline_mutation_screen" -> "携带胚系突变",
    "exist_risk" -> "携带胚系突变",
    "medium_high_risk" -> "携带胚系突变",
    //            "MMR_mutation" -> "MMR突变",
    //    "HR_mutation" -> "HR突变"
  )

  def main(args: Array[String]): Unit = {
    val startTime = System.currentTimeMillis()
    //        dataMerge
    //    mHDataMerge
    //    runScript
    resultDeal
    println((System.currentTimeMillis() - startTime) / 1000.0)

  }

  def isValidFileName(name: String, excludeSuffixs: List[String] = List.empty) = {
    val suffixs = List(".pdf", ".csv", ".svg", ".html", ".js", ".xlsx", "xls", "gml").filterNot { x =>
      excludeSuffixs.contains(x)
    }
    suffixs.exists(x => name.endsWith(x)) && (!name.startsWith("Rplots"))
  }

  def sameDirCopyRecu(sourceDir: File, targetDir: File) = {

    def loop(file: File): Unit = {
      for (file <- file.listFiles()) {
        if (file.isDirectory) {
          loop(file)
        } else {
          if (isValidFileName(file.getName)) {
            val filePath = file.getAbsolutePath
            val sourcePath = sourceDir.getAbsolutePath
            val targetFile = new File(targetDir, filePath.replace(sourcePath, ""))
            FileUtils.copyFile(file, targetFile)
          }

        }
      }
    }

    loop(sourceDir)

  }

  def resultDeal = {

    val resultDir = new File(parentDir, "result").createDirectoryWhenNoExist
    dirMap.foreach { case (dirName, columnName) =>
      val eachDir = new File(resultDir, dirName)
      val workspacePDir = new File(parentDir, dirName)
      val workspaceDir = new File(workspacePDir, "workspace")
      sameDirCopyRecu(workspaceDir, eachDir)
      val map = SeqMap("RF" -> "Random_Forest", "LR" -> "Logistic_Regression", "SVM" -> "Support_Vector_Machine",
        "cv" -> "Cross-Validation-Results")
      map.foreach { case (oldName, newName) =>
        eachDir.allFileAndDirs.filter(_.isDirectory).filter { x =>
          x.getName == oldName
        }.foreach { x =>
          val destDir = new File(x.getParentFile, newName).deleteIfExist
          x.dirMoveTo(destDir)
        }
      }
    }

  }

  def runScript = {
    dirMap.par.foreach { case (dirName, columnName) =>
      val dir = new File(parentDir, dirName).createDirectoryWhenNoExist
      val workspaceDir = new File(dir, "workspace")
      List("calculate_config.txt", "pr_plot_config.txt", "roc_plot_config.txt", "rfPlot_plot_config.txt").
        foreach { fileName =>
          new File(parentDir, fileName).fileCopyToDir(workspaceDir)
        }
      val roCvPlotConfigFile = new File(parentDir, "rocCv_plot_config.txt")
      val roCvEachPlotConfigFile = new File(parentDir, "rocCvEach_plot_config.txt")
      val commandExec = CommandExec().exec { b =>
        val groupFile = new File(dir, "group.xlsx")
        val dataFile = new File(dir, "data.xlsx")
        val command =
          s"""
             |cp ${groupFile.unixPath} group.xlsx
             |cp ${dataFile.unixPath} data.xlsx
             |${Tool.rScript} ${new File(Tool.rPath, "train_test_split.R").unixPath}
             |""".stripMargin
        val cvSplitCommand =
          s"""
             |${"Rscript".condaPath()} ${new File(Tool.rPath, "cv_split.R").unixPath}
             |""".stripMargin
        CommandData(workspaceDir, List(command, cvSplitCommand))
      }.parExec { b =>
        val lrMap = SeqMap("LR" -> "fs_logistic")
        val rfMap = SeqMap("RF" -> "fs_rf")
        val svmMap = SeqMap("SVM" -> "fs_svm")
        val map = lrMap ++ rfMap ++ svmMap
        map.flatMap { case (prefix, rName) =>
          val subWorkspaceDir = new File(workspaceDir, prefix)
          val command =
            s"""
               |dos2unix *
               |${"Rscript".condaPath()} ${new File(Tool.rPath, s"${rName}.R").unixPath}
               |${"Rscript".condaPath()} ${new File(Tool.rPath, s"${prefix.toLowerCase}_plot.R").unixPath}
               |${"Rscript".condaPath()} ${new File(Tool.rPath, "model_roc_plot.R").unixPath}
               |${"Rscript".condaPath()} ${new File(Tool.rPath, "pr_plot.R").unixPath}
               |""".stripMargin
          val cvDir = new File(subWorkspaceDir, "cv")
          val cvCommands = {
            val calculateCommand = prefix match {
              case "RF" =>
                s"""
                   |${"Rscript".condaPath()} ${new File(Tool.rPath, "rf_cv.R").unixPath}
                   |""".stripMargin
              case "GB" =>
                s"""
                   |${"Rscript".condaPath()} ${new File(Tool.rPath, "gb_cv.R").unixPath}
                   |""".stripMargin
              case "LR" =>
                s"""
                   |${"Rscript".condaPath()} ${new File(Tool.rPath, "lr_cv.R").unixPath}
                   |""".stripMargin
              case "SVM" =>
                s"""
                   |${"Rscript".condaPath()} ${new File(Tool.rPath, "svm_cv.R").unixPath}
                   |""".stripMargin
              case _ => ""
            }
            val plotCommand = {
              s"""
                 |${"Rscript".condaPath()} ${new File(Tool.rPath, "cv_roc_plot.R").unixPath} --pc ${roCvPlotConfigFile.unixPath}
                 |${"Rscript".condaPath()} ${new File(Tool.rPath, "cv_roc_all_plot.R").unixPath} --pc ${roCvEachPlotConfigFile.unixPath}
                 |""".stripMargin
            }
            List(calculateCommand, plotCommand)
          }
          List(CommandData(subWorkspaceDir, command), CommandData(cvDir, cvCommands))
        }.toList.par
      }
      if (!commandExec.isSuccess) {
        System.err.println(commandExec.errorInfo)
      }
    }
  }

  //  def dealNa = {
  //    val dataFile = new File(parentDir, "data.txt")
  //    val outFile = new File(parentDir, "data.csv")
  //    //    val commandExec = CommandExec().exec { b =>
  //    //      val command =
  //    //        s"""
  //    //           |${"Rscript".condaPath()} ${new File(Tool.rPath, s"transpose.R").unixPath}
  //    //           |""".stripMargin
  //    //      CommandData(parentDir, command)
  //    //    }
  //    dataFile.txtLines.toFile(outFile)
  //  }

  def dataMerge = {
    val inPath = new File("E:\\crc_database\\test")
    val basicFile = new File(inPath, s"湖州市卫生健康委员会_2020年任务_居民基本信息.txt")
    val questionFile = new File(inPath, "question.txt")
    val familyFile = new File(inPath, "family_history.txt")
    val biopsyFile = new File(inPath, "biopsy.txt")
    val familyDbMap = familyFile.txtLines.lineSeqMap.map { map =>
      map("SID") -> map
    }.toSeqMap
    val basicDbMap = basicFile.txtLines.lineSeqMap.map { map =>
      map("SID") -> map
    }.toSeqMap
    val questionDbMap = questionFile.txtLines.lineSeqMap.map { map =>
      map("SID") -> map
    }.toSeqMap
    val intestinalDiseases = questionDbMap.flatMap { case (sid, map) =>
      map("您曾被确诊的肠道疾病为（可多选）").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    val chronicDiseases = questionDbMap.flatMap { case (sid, map) =>
      map("您曾被确诊的慢性疾病为（可多选）").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    val drinks = questionDbMap.flatMap { case (sid, map) =>
      map("您每周饮酒的种类").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    val exerciseKinds = questionDbMap.flatMap { case (sid, map) =>
      map("您最常用的锻炼方式是哪种？").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    println(exerciseKinds)
    val typeMap1 = SeqMap("阿司匹林服药频率为每周几次" -> "num",
      "您经常吸烟吗（每天吸烟1支以上并连续或累计6个月以上）" -> "discrete",
      "您从多少岁开始养成每天都吸烟的习惯？" -> "num",
      "扣除戒烟年数，您一共吸烟多少年？" -> "num",
      "您平均每天吸烟多少支？" -> "num",
      "您是否在家中或工作场所吸入二手烟（每周1天以上，每天至少15分钟）" -> "discrete",
      "在有烟雾的室内环境中，您与吸烟者共同居住或工作了多少年" -> "num",
      "扣除戒烟年数，该吸烟者一共吸烟多少年" -> "num",
      "该吸烟者平均每天吸烟多少支" -> "num",
      "您经常饮酒吗（每周饮酒1次以上并连续6个月以上）" -> "discrete",
      "您每周有几天饮酒？" -> "num",
    )
    val typeMap2 = SeqMap(
      "白酒每周饮酒量" -> "num",
      "新鲜蔬菜食用频率" -> "discrete",
      "新鲜水果食用频率" -> "discrete",
      "新鲜红肉类（猪、牛、羊）食用频率" -> "discrete",
      "新鲜白肉类（禽、鱼、虾、蟹、贝）食用频率" -> "discrete",
      "豆类及豆制品（含豆芽、豆浆）食用频率" -> "discrete",
      "腌制蔬菜（咸菜、梅干菜、榨菜、泡菜）食用频率" -> "discrete",
      "加工肉类（腊肉、香肠、火腿、咸鱼）食用频率" -> "discrete",
      "油炸或烧烤食品食用频率" -> "discrete",
      "全谷物（全小麦、糙米、大麦、燕麦、荞麦、高粱、黑米、小米、玉米）食用频率" -> "discrete",
      "在过去1年里，您业余时间一般多久参加1次体育锻炼" -> "discrete",
    )
    val typeMap3 = SeqMap(
      "您每周参加体育锻炼累计多长时间？" -> "num",
      "您的血缘亲属中，是否有人曾被确诊恶性肿瘤" -> "discrete",
    )
    val outHeaders = List("SampleID", "年龄", "性别", "直系亲属是否患癌", "BMI", "腰围", "受教育程度",
      "您现在是否存在腹部不适或其他肠道异常的症状", "腹痛", "腹胀", "腹部肿块", "排便异常", "便血", "6个月内缺铁性贫血",
      "体重减轻", "您是否曾经进行粪便潜血试验（FOBT）检测", "粪便潜血试验（FOBT）检测结果为", "您是否曾被确诊患有恶性肿瘤",
      "您是否曾被确诊肠道疾病") ::: intestinalDiseases ::: List("您是否进行过胆囊切除手术", "您是否曾被确诊慢性疾病") :::
      chronicDiseases ::: typeMap1.keyList ::: drinks ::: typeMap2.keyList ::: exerciseKinds ::: typeMap3.keyList
    val outLines = biopsyFile.txtLines.lineSeqMap.map { map =>
      val sid = map("SID")
      val basicMap = basicDbMap(sid)
      val evalDateStr = basicMap("评估日期").mySplit("\\s+").head
      val birthDateStr = basicMap("出生日期")
      val age = if (evalDateStr.isBlank || birthDateStr.isBlank) {
        ""
      } else {
        val evalDate = Utils.string2DateTime(evalDateStr, pattern = "yyyy-MM-dd")
        val formatBirthDate = s"${birthDateStr.take(4)}-${birthDateStr.drop(4).take(2)}-${birthDateStr.takeRight(2)}"
        val birthDate = Utils.string2DateTime(s"${formatBirthDate}", pattern = "yyyy-MM-dd")
        val age = Years.yearsBetween(birthDate, evalDate).getYears
        age.toString
      }

      val familyMap = familyDbMap(sid)
      val familyHasCancer = if (familyMap("亲属关系").nonBlank || familyMap("恶性肿瘤类型").nonBlank ||
        familyMap("其他恶性肿瘤类型").nonBlank) {
        "1"
      } else "0"
      val questionMap = questionDbMap(sid)
      val height = questionMap("身高（厘米）")
      val weight = questionMap("体重（千克）")
      val bmi = weight.toDouble / math.pow(height.toDouble / 100, 2)
      val schooling = questionMap("受教育程度").mySplit("=").head
      val hasDiscomfort = questionMap("您现在是否存在腹部不适或其他肠道异常的症状").mySplit("=").head
      val mainSymptoms = questionMap("您现在出现的主要症状为（可多选）").mySplit(",")
      val hasStomachache = if (mainSymptoms.contains("1=腹痛")) "1" else "0"
      val hasBloating = if (mainSymptoms.contains("2=腹胀")) "1" else "0"
      val abdominalLumps = if (mainSymptoms.contains("3=腹部肿块")) "1" else "0"
      val mainSymptom4 = if (mainSymptoms.exists(_.contains("4="))) "1" else "0"
      val mainSymptom5 = if (mainSymptoms.exists(_.contains("5="))) "1" else "0"
      val mainSymptom6 = if (mainSymptoms.exists(_.contains("6="))) "1" else "0"
      val mainSymptom7 = if (mainSymptoms.exists(_.contains("7="))) "1" else "0"
      val fobt = questionMap("您是否曾经进行粪便潜血试验（FOBT）检测").mySplit("=").head
      val fobtResult = questionMap("粪便潜血试验（FOBT）检测结果为").mySplit("=").head
      val tumor = questionMap("您是否曾被确诊患有恶性肿瘤").mySplit("=").head
      val bowel = questionMap("您是否曾被确诊肠道疾病").mySplit("=").head
      val intestinalDisease = questionMap("您曾被确诊的肠道疾病为（可多选）").mySplit(",")
      val intestinalDiseases = (1 to 9).toList.map { i =>
        if (intestinalDisease.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val cholecystectomy = questionMap("您是否进行过胆囊切除手术").mySplit("=").head
      val chronicDiseases = questionMap("您是否曾被确诊慢性疾病").mySplit("=").head
      val exerciseKind = questionMap("您最常用的锻炼方式是哪种？").mySplit(",")
      val exerciseKinds = (1 to 6).toList.map { i =>
        if (exerciseKind.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val typeValue1 = typeMap1.map { case (columnName, kind) =>
        kind match {
          case "num" =>
            val str = questionMap(columnName)
            if (str.isBlank) "0" else str
          case "discrete" =>
            questionMap(columnName).mySplit("=").head
        }
      }.toList
      val drink = questionMap("您每周饮酒的种类").mySplit(",")
      val drinks = (1 to 3).toList.map { i =>
        if (drink.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val typeValue2 = typeMap2.map { case (columnName, kind) =>
        kind match {
          case "num" =>
            val str = questionMap(columnName)
            if (str.isBlank) "0" else str
          case "discrete" =>
            questionMap(columnName).mySplit("=").head
        }
      }.toList
      val detailChronicDiseases = questionMap("您曾被确诊的慢性疾病为（可多选）").mySplit(",")
      val detailChronicDiseaseses = (1 to 6).toList.map { i =>
        if (detailChronicDiseases.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val typeValue3 = typeMap3.map { case (columnName, kind) =>
        kind match {
          case "num" =>
            val str = questionMap(columnName)
            if (str.isBlank) "0" else str
          case "discrete" =>
            questionMap(columnName).mySplit("=").head
        }
      }.toList
      List(s"${sid}", age, basicMap("性别"), familyHasCancer, bmi.toString, questionMap("腰围（厘米）"), schooling,
        hasDiscomfort, hasStomachache, hasBloating, abdominalLumps, mainSymptom4, mainSymptom5, mainSymptom6,
        mainSymptom7, fobt, fobtResult, tumor, bowel) ::: intestinalDiseases :::
        List(cholecystectomy, chronicDiseases) ::: detailChronicDiseaseses ::: typeValue1 ::: drinks ::: typeValue2 :::
        exerciseKinds ::: typeValue3
    }
    val newLines = (outHeaders :: outLines).map { columns =>
      columns.map { column =>
        if (column.isBlank) "N_A" else column
      }
    }.distinctByKeepLast(_.head)
    newLines.transpose.toXlsxFile(new File(inPath, "data.xlsx"))
    biopsyFile.txtLines.lineSeqMap.map { map =>
      val risk = if (map("镜下考虑病变类型").isBlank) {
        "低风险"
      } else "风险"
      SeqMap("SampleID" -> s"${map("SID")}", "Group" -> risk)
    }.csvLines.distinctByKeepLast(_.head).toXlsxFile(new File(inPath, "group.xlsx"))

  }

  def mHDataMerge = {
    val inPath = new File("E:\\crc_database\\test")
    val basicFile = new File(inPath, s"湖州市卫生健康委员会_2020年任务_居民基本信息.txt")
    val questionFile = new File(inPath, "question.txt")
    val familyFile = new File(inPath, "family_history.txt")
    val biopsyFile = new File(inPath, "biopsy.txt")
    val familyDbMap = familyFile.txtLines.lineSeqMap.map { map =>
      map("SID") -> map
    }.toSeqMap
    val basicDbMap = basicFile.txtLines.lineSeqMap.map { map =>
      map("SID") -> map
    }.toSeqMap
    val questionDbMap = questionFile.txtLines.lineSeqMap.map { map =>
      map("SID") -> map
    }.toSeqMap
    val intestinalDiseases = questionDbMap.flatMap { case (sid, map) =>
      map("您曾被确诊的肠道疾病为（可多选）").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    val chronicDiseases = questionDbMap.flatMap { case (sid, map) =>
      map("您曾被确诊的慢性疾病为（可多选）").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    val drinks = questionDbMap.flatMap { case (sid, map) =>
      map("您每周饮酒的种类").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    val exerciseKinds = questionDbMap.flatMap { case (sid, map) =>
      map("您最常用的锻炼方式是哪种？").mySplit(",")
    }.toList.distinct.notEmptyLines.sortBy(_.mySplit("=").head.toInt).map(_.mySplit("=").last)
    println(exerciseKinds)
    val typeMap1 = SeqMap("阿司匹林服药频率为每周几次" -> "num",
      "您经常吸烟吗（每天吸烟1支以上并连续或累计6个月以上）" -> "discrete",
      "您从多少岁开始养成每天都吸烟的习惯？" -> "num",
      "扣除戒烟年数，您一共吸烟多少年？" -> "num",
      "您平均每天吸烟多少支？" -> "num",
      "您是否在家中或工作场所吸入二手烟（每周1天以上，每天至少15分钟）" -> "discrete",
      "在有烟雾的室内环境中，您与吸烟者共同居住或工作了多少年" -> "num",
      "扣除戒烟年数，该吸烟者一共吸烟多少年" -> "num",
      "该吸烟者平均每天吸烟多少支" -> "num",
      "您经常饮酒吗（每周饮酒1次以上并连续6个月以上）" -> "discrete",
      "您每周有几天饮酒？" -> "num",
    )
    val typeMap2 = SeqMap(
      "白酒每周饮酒量" -> "num",
      "新鲜蔬菜食用频率" -> "discrete",
      "新鲜水果食用频率" -> "discrete",
      "新鲜红肉类（猪、牛、羊）食用频率" -> "discrete",
      "新鲜白肉类（禽、鱼、虾、蟹、贝）食用频率" -> "discrete",
      "豆类及豆制品（含豆芽、豆浆）食用频率" -> "discrete",
      "腌制蔬菜（咸菜、梅干菜、榨菜、泡菜）食用频率" -> "discrete",
      "加工肉类（腊肉、香肠、火腿、咸鱼）食用频率" -> "discrete",
      "油炸或烧烤食品食用频率" -> "discrete",
      "全谷物（全小麦、糙米、大麦、燕麦、荞麦、高粱、黑米、小米、玉米）食用频率" -> "discrete",
      "在过去1年里，您业余时间一般多久参加1次体育锻炼" -> "discrete",
    )
    val typeMap3 = SeqMap(
      "您每周参加体育锻炼累计多长时间？" -> "num",
      "您的血缘亲属中，是否有人曾被确诊恶性肿瘤" -> "discrete",
    )
    val outHeaders = List("SampleID", "年龄", "性别", "直系亲属是否患癌", "BMI", "腰围", "受教育程度",
      "您现在是否存在腹部不适或其他肠道异常的症状", "腹痛", "腹胀", "腹部肿块", "排便异常", "便血", "6个月内缺铁性贫血",
      "体重减轻", "您是否曾经进行粪便潜血试验（FOBT）检测", "粪便潜血试验（FOBT）检测结果为", "您是否曾被确诊患有恶性肿瘤",
      "您是否曾被确诊肠道疾病") ::: intestinalDiseases ::: List("您是否进行过胆囊切除手术", "您是否曾被确诊慢性疾病") :::
      chronicDiseases ::: typeMap1.keyList ::: drinks ::: typeMap2.keyList ::: exerciseKinds ::: typeMap3.keyList
    val outLines = biopsyFile.txtLines.lineSeqMap.filter { map =>
      map("镜下考虑病变类型").nonBlank
    }.map { map =>
      val sid = map("SID")
      val basicMap = basicDbMap(sid)
      val evalDateStr = basicMap("评估日期").mySplit("\\s+").head
      val birthDateStr = basicMap("出生日期")
      val age = if (evalDateStr.isBlank || birthDateStr.isBlank) {
        ""
      } else {
        val evalDate = Utils.string2DateTime(evalDateStr, pattern = "yyyy-MM-dd")
        val formatBirthDate = s"${birthDateStr.take(4)}-${birthDateStr.drop(4).take(2)}-${birthDateStr.takeRight(2)}"
        val birthDate = Utils.string2DateTime(s"${formatBirthDate}", pattern = "yyyy-MM-dd")
        val age = Years.yearsBetween(birthDate, evalDate).getYears
        age.toString
      }

      val familyMap = familyDbMap(sid)
      val familyHasCancer = if (familyMap("亲属关系").nonBlank || familyMap("恶性肿瘤类型").nonBlank ||
        familyMap("其他恶性肿瘤类型").nonBlank) {
        "1"
      } else "0"
      val questionMap = questionDbMap(sid)
      val height = questionMap("身高（厘米）")
      val weight = questionMap("体重（千克）")
      val bmi = weight.toDouble / math.pow(height.toDouble / 100, 2)
      val schooling = questionMap("受教育程度").mySplit("=").head
      val hasDiscomfort = questionMap("您现在是否存在腹部不适或其他肠道异常的症状").mySplit("=").head
      val mainSymptoms = questionMap("您现在出现的主要症状为（可多选）").mySplit(",")
      val hasStomachache = if (mainSymptoms.contains("1=腹痛")) "1" else "0"
      val hasBloating = if (mainSymptoms.contains("2=腹胀")) "1" else "0"
      val abdominalLumps = if (mainSymptoms.contains("3=腹部肿块")) "1" else "0"
      val mainSymptom4 = if (mainSymptoms.exists(_.contains("4="))) "1" else "0"
      val mainSymptom5 = if (mainSymptoms.exists(_.contains("5="))) "1" else "0"
      val mainSymptom6 = if (mainSymptoms.exists(_.contains("6="))) "1" else "0"
      val mainSymptom7 = if (mainSymptoms.exists(_.contains("7="))) "1" else "0"
      val fobt = questionMap("您是否曾经进行粪便潜血试验（FOBT）检测").mySplit("=").head
      val fobtResult = questionMap("粪便潜血试验（FOBT）检测结果为").mySplit("=").head
      val tumor = questionMap("您是否曾被确诊患有恶性肿瘤").mySplit("=").head
      val bowel = questionMap("您是否曾被确诊肠道疾病").mySplit("=").head
      val intestinalDisease = questionMap("您曾被确诊的肠道疾病为（可多选）").mySplit(",")
      val intestinalDiseases = (1 to 9).toList.map { i =>
        if (intestinalDisease.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val cholecystectomy = questionMap("您是否进行过胆囊切除手术").mySplit("=").head
      val chronicDiseases = questionMap("您是否曾被确诊慢性疾病").mySplit("=").head
      val exerciseKind = questionMap("您最常用的锻炼方式是哪种？").mySplit(",")
      val exerciseKinds = (1 to 6).toList.map { i =>
        if (exerciseKind.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val typeValue1 = typeMap1.map { case (columnName, kind) =>
        kind match {
          case "num" =>
            val str = questionMap(columnName)
            if (str.isBlank) "0" else str
          case "discrete" =>
            questionMap(columnName).mySplit("=").head
        }
      }.toList
      val drink = questionMap("您每周饮酒的种类").mySplit(",")
      val drinks = (1 to 3).toList.map { i =>
        if (drink.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val typeValue2 = typeMap2.map { case (columnName, kind) =>
        kind match {
          case "num" =>
            val str = questionMap(columnName)
            if (str.isBlank) "0" else str
          case "discrete" =>
            questionMap(columnName).mySplit("=").head
        }
      }.toList
      val detailChronicDiseases = questionMap("您曾被确诊的慢性疾病为（可多选）").mySplit(",")
      val detailChronicDiseaseses = (1 to 6).toList.map { i =>
        if (detailChronicDiseases.exists(_.contains(s"${i}="))) "1" else "0"
      }
      val typeValue3 = typeMap3.map { case (columnName, kind) =>
        kind match {
          case "num" =>
            val str = questionMap(columnName)
            if (str.isBlank) "0" else str
          case "discrete" =>
            questionMap(columnName).mySplit("=").head
        }
      }.toList
      List(s"${sid}", age, basicMap("性别"), familyHasCancer, bmi.toString, questionMap("腰围（厘米）"), schooling,
        hasDiscomfort, hasStomachache, hasBloating, abdominalLumps, mainSymptom4, mainSymptom5, mainSymptom6,
        mainSymptom7, fobt, fobtResult, tumor, bowel) ::: intestinalDiseases :::
        List(cholecystectomy, chronicDiseases) ::: detailChronicDiseaseses ::: typeValue1 ::: drinks ::: typeValue2 :::
        exerciseKinds ::: typeValue3
    }
    val newLines = (outHeaders :: outLines).map { columns =>
      columns.map { column =>
        if (column.isBlank) "N_A" else column
      }
    }.distinctByKeepLast(_.head)
    val outDir = new File(inPath, "medium_high_risk")
    newLines.transpose.toXlsxFile(new File(outDir, "data.xlsx"))
    biopsyFile.txtLines.lineSeqMap.filter { map =>
      map("镜下考虑病变类型").nonBlank
    }.map { map =>
      val risk = if ((map("最大径").isDouble && map("最大径").toDouble > 1) || map("镜下考虑病变类型").equals("疑似癌")) {
        "高风险"
      } else "中风险"
      SeqMap("SampleID" -> s"${map("SID")}", "Group" -> risk)
    }.csvLines.distinctByKeepLast(_.head).toXlsxFile(new File(outDir, "group.xlsx"))

  }


}
